﻿using System.Data.SqlClient;
using System;
namespace DAO
{
    public class DataProvider
    {
        private static string dataBase;

        public static string DataBase
        {
            get { return dataBase; }
            set { dataBase = value; }
        }
        private static string namePC;

        public static string NamePC
        {
            get { return namePC; }
            set { namePC = value; }
        }
        private static string uID;

        public static string UID
        {
            get { return uID; }
            set { uID = value; }
        }
        private static string pwd;

        public static string Pwd
        {
            get { return pwd; }
            set { pwd = value; }
        }

        public static SqlConnection ConnectDB2()
        {
            string s;
            if (uID == null)
                s = "Data Source=" + namePC + ";Integrated Security=SSPI;";
            else
                s = "Data Source=" + namePC + ";Integrated Security=SSPI;uid="
                    + uID + ";pwd=" + pwd + ";";
            SqlConnection conn;
            conn = new SqlConnection(s);
            conn.Open();
            return conn;
        }
        public static SqlConnection ConnectDB()
        {
            string s;
            if (uID == null)
                s = "Data Source=" + namePC + ";Integrated Security=SSPI;Initial Catalog=" + dataBase;
            else
                s = "Data Source=" + namePC + ";Integrated Security=SSPI;Initial Catalog=" + dataBase +";uid="
                    + uID +";pwd="+pwd+";";
            SqlConnection conn;
            conn = new SqlConnection(s);
            conn.Open();
            return conn;
        }
        public static bool isConnected()
        {
            string s;
            if (uID == null)
                s = "Data Source=" + namePC + ";Integrated Security=SSPI;Initial Catalog=" + dataBase;
            else
                s = "Data Source=" + namePC + ";Integrated Security=SSPI;Initial Catalog=" + dataBase + ";uid="
                    + uID + ";pwd=" + pwd + ";";
            SqlConnection conn;
            conn = new SqlConnection(s);
            try
            {
                conn.Open();
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                return false;
            }
        }
    }
}
